package auto.app.controller;

import auto.app.util.FilesUtil;
import auto.app.util.OSSTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/common")
@Api(value = "公共方法",tags = "公共方法")
@Slf4j
public class CommonController {

    @Autowired
    private OSSTemplate ossTemplate;

    @GetMapping(value = "/downloadFile",name = "下载文件")
    @ApiOperation(value = "下载文件",notes = "下载文件")
    public void exportNew(HttpServletResponse response) throws Exception {
        String s = ossTemplate.downloadFile("2023-01-29/数据网关-内部资料-v1.0.docx");
        byte[] bytes = FilesUtil.downFileByUrl(s);
        long beginTime = System.currentTimeMillis();
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        byteArrayOutputStream.write(bytes);
        long endTime = System.currentTimeMillis();
        log.info((endTime-beginTime)+"毫秒");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("测试报告.docx", "UTF-8"));
        response.setContentType("application/pdf");
        response.setContentLength(byteArrayOutputStream.size());
        ServletOutputStream outputStream = response.getOutputStream();
        byteArrayOutputStream.writeTo(outputStream);
        byteArrayOutputStream.close();
    }
}
